package com.demo.http;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSONObject;

import io.netty.handler.codec.http.HttpRequest;

public class RequestParse {

	private String uri;

	private String contentType = "text/html";

	private byte[] content;
	
	public RequestParse(Object msg) {
		HttpRequest req = (HttpRequest) msg;
		this.uri = req.uri();
		if(uri!=null&&uri.length()>0){
			String suffix = uri.substring(uri.indexOf(".")+1, uri.length());
			if("json".equals(suffix)){
				contentType = "application/json";
				JSONObject object = new JSONObject();
				object.put("success", true);
				content = object.toJSONString().getBytes();
			}else{
				File f = new File(getPath()+uri);
				System.out.println(f.getAbsolutePath());
				try {
					content = FileUtils.readFileToByteArray(f);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String getUri() {
		return uri;
	}

	public String getContentType() {
		return contentType;
	}

	public byte[] getContent() {
		return content;
	}
	
	private static String path = "";
	private static String getPath(){
		if(path.equals("")){
			path = Class.class.getClass().getResource("/").getPath();
		}
		System.out.println(path);
		return path;
	}

}
